package com.lucky.carcodepro.domain.response;

import com.lucky.carcodepro.domain.enums.ResponseStatusEnums;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * Response
 * SDK 通用返回体
 *
 * @author Annie
 * @version 2024/10/2 14:53
 **/
@Data
@NoArgsConstructor
public class Response<T>
{
    /**
     * 业务识别码
     */
    private String code;
    /**
     * 提示信息
     */
    private String msg;
    /**
     * 返回数据
     */
    private T data;

    private Response(String code, String msg, T data)
    {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private Response(ResponseStatusEnums statusEnums, T data)
    {
        this.code = statusEnums.getCode();
        this.msg = statusEnums.getMsg();
        this.data = data;
    }

    public static <T> Response<T> success(String code, String msg, T data)
    {
        return new Response<>(code, msg, data);
    }

    public static <T> Response<T> success(String code, String msg)
    {
        return new Response<>(code, msg, null);
    }

    public static <T> Response<T> success(T data)
    {
        return new Response<>(ResponseStatusEnums.SUCCESS, data);
    }

    public static <T> Response<T> success(ResponseStatusEnums enums)
    {
        return new Response<>(enums.getCode(), enums.getMsg(), null);
    }

    public static <T> Response<T> success()
    {
        return new Response<>(ResponseStatusEnums.SUCCESS, null);
    }

    /**
     * 请求是否是成功的
     *
     * @return 结果
     */
    public Boolean ifSuccess()
    {
        return Objects.equals(code, ResponseStatusEnums.SUCCESS.getCode());
    }
}